package com.intube.sso.servcie;

import com.intube.sso.entity.SsoUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author YDF
 * @date 2020/4/9
 */
@Slf4j
@Component
public class SsoUserDetailService implements UserDetailsService {

    private final PasswordEncoder passwordEncoder;

    public SsoUserDetailService(@Autowired PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public UserDetails loadUserByUsername(String user) throws UsernameNotFoundException {
        SsoUser result = new SsoUser(user, passwordEncoder.encode("123456"), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
        log.info("用户[{}]登录完成", result.getUsername());
        return result;
    }
}
